//
//  ST_MyPhotoCollectionCell.swift
//  btvideo
//
//  Created by mac on 2020/1/6.
//  Copyright © 2020 JarvanZhang. All rights reserved.
//

import UIKit
import SnapKit
import SwifterSwift

class ST_MyPhotoCollectionCell: UICollectionViewCell {
    
    override init(frame: CGRect) {
           
        super.init(frame: frame)
        
        self.contentView.backgroundColor = Color.white
        
//        self.contentView.backgroundColor = Color.random
    
        
        contentView.addSubview(cover_image_view)
        cover_image_view.snp.makeConstraints { (make) in
            make.edges.equalToSuperview()
        }
        
        self.contentView.addSubview(uploading_mas_view)
        
        uploading_mas_view.snp.makeConstraints { (make) in
            make.edges.equalToSuperview()
        }
        self.uploading_mas_view.addSubview(indicator)
        self.uploading_mas_view.addSubview(indicator_label)
        indicator_label.snp.makeConstraints { (make) in
            make.centerX.equalToSuperview()
            make.centerY.equalToSuperview().offset(20)
        }
        indicator.snp.makeConstraints { (make) in
            make.centerX.equalToSuperview()
            make.bottom.equalTo(indicator_label.snp.top).offset(-5)
            make.width.height.equalTo(44)
        }
        
        self.contentView.addSubview(upload_error_btn)
        upload_error_btn.snp.makeConstraints { (make) in
            make.edges.equalToSuperview()
        }
        
        self.contentView.addSubview(cover_mask_view)
        cover_mask_view.snp.makeConstraints { (make) in
            make.edges.equalToSuperview()
        }
        cover_mask_view.addSubview(mask_label)
        mask_label.snp.makeConstraints { (make) in
            make.edges.equalToSuperview()
        }
        
        self.contentView.addSubview(delete_btn)
        
        delete_btn.addTarget(self, action: #selector(delete_handle), for: UIControl.Event.touchUpInside)
        
    }
    
    override func layoutSubviews() {
        super.layoutSubviews()

        let w = self.bounds.width
        if w < 200 {
            delete_btn.frame = CGRect.init(x: w - 32 - 5, y: 5, width: 32, height: 32)
        }else{
            delete_btn.frame = CGRect.init(x: w - 42 - 11, y: 11, width: 42, height: 42)
        }
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
//    MARK: - Properties
    fileprivate lazy var cover_image_view:UIImageView = {
        let control = UIImageView()
        control.contentMode = UIView.ContentMode.scaleAspectFill
        control.cornerRadius = 20
        return control
    }()
    
    fileprivate var upload_error_btn:UIButton = {
        
        let control = UIButton.init(type: .custom)
        control.setImageForAllStates(ST_ImageUtils.imageName("n_私信_icon_发送失败")!)
        
        control.add_touch_up(insideTarget: self, action: #selector(re_send_message))
        
        control.isHidden = true
        return control
    }()
    
    fileprivate var delete_btn:UIButton = {
        
        let control = UIButton.init(type: .custom)
        
        control.setImageForAllStates(ST_ImageUtils.imageName("个人中心_icon_删除84")!)
        
        return control
    }()
    
    
    
    
    fileprivate lazy var uploading_mas_view:UIView = {
        let control = UIView()
        control.backgroundColor = Color.gray
        control.alpha = 0.8
        control.cornerRadius = 20
        control.isHidden = true
        return control
    }()
    
    fileprivate lazy var indicator:UIActivityIndicatorView = {
        
        let control = UIActivityIndicatorView()
        
        return control
        
    }()
    fileprivate lazy var indicator_label:UILabel = {
        
        let control = UILabel()
        control.textColor = Color.white
        control.font = UIFont.systemFont(ofSize: 14)
        control.textAlignment = .center
        return control
    }()
    
    fileprivate lazy var cover_mask_view:UIView = {
        let control = UIView()
        control.backgroundColor = UIColor.init(white: 0, alpha: 0.4)
        control.isHidden = true
        control.cornerRadius = 20
        return control
    }()
    
    fileprivate lazy var mask_label:UILabel = {
       let control = UILabel()
        control.text = "审核中.."
        control.font = UIFont.systemFont(ofSize: 14, weight: UIFont.Weight.semibold)
        control.textColor = .white
        control.textAlignment = .center
        return control
    }()
    
    
    
    func setting_uplaod_status(){
        
//            功能 #3913
//        审核中 先判断
        /*
        if model.state == 1{
         
            
            uploading_mas_view.isHidden = true
            upload_error_btn.isHidden = true
            delete_btn.isHidden = false
            indicator.stopAnimating()
            
            cover_mask_view.isHidden = false
            
            return
        }*/
        
        cover_mask_view.isHidden = true
        
            
        if model.uploadStatus == .success{
            uploading_mas_view.isHidden = true
            upload_error_btn.isHidden = true
            delete_btn.isHidden = false
            indicator.stopAnimating()
            //                    print("A9999success")
        }else if model.uploadStatus == .failure{
            uploading_mas_view.isHidden = true
            self.upload_error_btn.isHidden = false
            delete_btn.isHidden = false
            indicator.stopAnimating()
            
        }else{
            upload_error_btn.isHidden = true
            uploading_mas_view.isHidden = false
            indicator.startAnimating()
            
            indicator_label.text = "\(model.uploadImageProgress)%"
            
            
            delete_btn.isHidden = true
        }
        
        
//        print("当前进度条:",message.uploadImageProgress)
        
    }
    
 
    var deleteClosure:((ST_MyPhotoCollectionCell)->())?
    
    
//    MARK: - Bind Model
    var model = ST_MyPhotoCollectionCellModel(){
        didSet{
            
            if let img = model.localImage{
                cover_image_view.image = img
            }else{
                
                cover_image_view.pin_setImage(from: model.content.compress_image().url, placeholderImage: ST_SwiftConst.shared.fetch_random_place_holder_image())
            }
            setting_uplaod_status()
        }
    }
    
    
}

//MARK:- Event
extension ST_MyPhotoCollectionCell{
    
    @objc func re_send_message(){
        
        
        print("重新发送资源")
        
    }
    
    
    
    
    @objc func delete_handle(){
        
        
        print("删除资源")
        
        self.deleteClosure?(self)
        
        
    }
    
}





//MARK: - Add Style Cell
class MyPhotoAddButtonCell: UICollectionViewCell {
    
    override init(frame: CGRect) {
           
        super.init(frame: frame)
        
        self.contentView.backgroundColor = Color.init(hexString: "#F2F4F7")
        
        self.contentView.addSubview(add_avatar_btn)
        
        
        contentView.cornerRadius = 20
        
        add_avatar_btn.snp.makeConstraints { (make) in
            make.edges.equalToSuperview()
        }
        
        contentView.addSubview(animate_image_view)
        animate_image_view.snp.makeConstraints { (make) in
            make.bottom.equalToSuperview().offset(-12)
            make.centerX.equalToSuperview()
            make.width.equalTo(104)
            make.height.equalTo(29)
        }
        
//        add_avatar_btn.addTarget(self, action: #selector(add_handle), for: UIControl.Event.touchUpInside)
        
//        main_thread_execute(afterDelay: 1) {
            
//        }
    }
    
   
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
//    MARK: - Properties
    
    fileprivate lazy var add_avatar_btn:UIButton = {
        
        let control = UIButton.init(type: .custom)
        control.isUserInteractionEnabled = false
        control.setImageForAllStates(ST_ImageUtils.imageName("个人中心_icon_添加照片")!)
        return control
    }()
    
    fileprivate lazy var animate_image_view:UIButton = {
        
        let control = UIButton.init(type: UIButton.ButtonType.custom)
        control.isUserInteractionEnabled = false
        control.set_back_image_for_all_states(ST_ImageUtils.imageName("形状结合")!)
        control.setTitleForAllStates("再添加4张照片吧")
        control.titleLabel?.font = UIFont.systemFont(ofSize: 12, weight: UIFont.Weight.regular)
        control.titleEdgeInsets = UIEdgeInsets.init(top: 4, left: 0, bottom: 0, right: 0)
        control.setTitleColorForAllStates(UIColor.white)
        control.isHidden = true
        return control
    }()
    
    
    override func willMove(toSuperview newSuperview: UIView?) {
        super.willMove(toSuperview: newSuperview)
        
        if newSuperview == nil{
            self.animate_image_view.layer.removeAllAnimations()
        }
        
    }
    
    fileprivate var restoreTransform = false
    
    fileprivate func bounce_animation(){
       
        
        UIView.animate(withDuration: 0.75, animations: {
            
            self.animate_image_view.transform = CGAffineTransform.init(translationX: 0, y: 4)
            
        }) { (f) in
            
            
            UIView.animate(withDuration: 0.55, animations: {
                
                self.animate_image_view.transform = CGAffineTransform.identity
                
            }) { (f) in
                
//                main_thread_execute(afterDelay: 0.5) {
                    self.bounce_animation()
//                }
            }
            
        }
        
        
//        UIView.animate(withDuration: 0.6, delay: 1, usingSpringWithDamping: 1, initialSpringVelocity: 0.5, options: [.curveEaseInOut], animations: animations, completion:  { (f) in
//
////            self.restoreTransform = !self.restoreTransform
////
////            self.bounce_animation()5
//
//
//
//
//        })
        
    }
    
    
    
    var current_photo_count = 0{
        didSet{
            if current_photo_count < 5{
                self.animate_image_view.isHidden = false
                self.bounce_animation()
            }else{
                self.animate_image_view.isHidden = true
                self.animate_image_view.layer.removeAllAnimations()
            }
        }
    }
}

